function [shannon_limits, capacity_comparison, fig_shannon] = shannon_limit(EbN0_dB, channel_scenarios)
% Shannon限界分析
% 输入参数：
%   EbN0_dB - 信噪比范围 (dB)
%   channel_scenarios - 信道场景 ('awgn', 'rayleigh', 'mimo', 'multiuser')
% 输出参数：
%   shannon_limits - Shannon限界结果
%   capacity_comparison - 容量比较
%   fig_shannon - 图形句柄

% 添加路径
addpath('../Common');
colors = color_definitions();

% 参数设置
bandwidth = 180e3; % 带宽 (Hz)
noise_power_dBm = -174 + 10*log10(bandwidth);
noise_power = 10^((noise_power_dBm-30)/10);

% 初始化结果数组
num_snr = length(EbN0_dB);
num_scenarios = length(channel_scenarios);
shannon_limits = zeros(num_snr, num_scenarios);
capacity_comparison = zeros(num_scenarios, 3); % [理论容量, 实际容量, 效率]

fprintf('Shannon限界分析...\n');

% Shannon限界计算
for snr_idx = 1:num_snr
    snr_linear = 10^(EbN0_dB(snr_idx)/10);
    
    for scenario_idx = 1:num_scenarios
        scenario = channel_scenarios{scenario_idx};
        
        switch lower(scenario)
            case 'awgn'
                % AWGN信道Shannon限界
                shannon_limits(snr_idx, scenario_idx) = bandwidth * log2(1 + snr_linear);
                
            case 'rayleigh'
                % 瑞利衰落信道 (平均容量)
                % 使用近似公式：E[log2(1 + SNR*|h|^2)]
                num_realizations = 1000;
                channel_realizations = sqrt(0.5) * (randn(num_realizations, 1) + 1i * randn(num_realizations, 1));
                
                instantaneous_capacities = zeros(num_realizations, 1);
                for realization = 1:num_realizations
                    h = abs(channel_realizations(realization));
                    instantaneous_snr = snr_linear * h^2;
                    if instantaneous_snr > 0
                        instantaneous_capacities(realization) = bandwidth * log2(1 + instantaneous_snr);
                    end
                end
                shannon_limits(snr_idx, scenario_idx) = mean(instantaneous_capacities);
                
            case 'mimo'
                % MIMO信道 (4x4 IID)
                Nt = 4; Nr = 4;
                num_realizations = 100;
                
                total_capacity = 0;
                for realization = 1:num_realizations
                    % IID信道矩阵
                    H = sqrt(0.5) * (randn(Nr, Nt) + 1i * randn(Nr, Nt));
                    
                    % 奇异值分解
                    [U, S, V] = svd(H);
                    singular_values = diag(S);
                    
                    % 等功率分配容量
                    capacity_realization = 0;
                    for stream = 1:min(Nt, Nr)
                        if singular_values(stream) > 0
                            power_per_stream = snr_linear / Nt;
                            stream_snr = power_per_stream * singular_values(stream)^2;
                            capacity_realization = capacity_realization + bandwidth * log2(1 + stream_snr);
                        end
                    end
                    total_capacity = total_capacity + capacity_realization;
                end
                shannon_limits(snr_idx, scenario_idx) = total_capacity / num_realizations;
                
            case 'multiuser'
                % 多用户系统 (8用户)
                K = 8;
                num_realizations = 100;
                
                total_capacity = 0;
                for realization = 1:num_realizations
                    % 随机用户信道
                    user_channels = sqrt(0.5) * (randn(K, 1) + 1i * randn(K, 1));
                    
                    % 最大CQI调度 (选择最佳用户)
                    [~, best_user] = max(abs(user_channels));
                    best_snr = snr_linear * abs(user_channels(best_user))^2;
                    
                    if best_snr > 0
                        total_capacity = total_capacity + bandwidth * log2(1 + best_snr);
                    end
                end
                shannon_limits(snr_idx, scenario_idx) = total_capacity / num_realizations;
                
            otherwise
                % 默认为AWGN
                shannon_limits(snr_idx, scenario_idx) = bandwidth * log2(1 + snr_linear);
        end
    end
end

% 容量比较分析 (在固定SNR处)
snr_comparison = round(num_snr/2); % 中等SNR
for scenario_idx = 1:num_scenarios
    theoretical_capacity = shannon_limits(snr_comparison, scenario_idx);
    
    % 简化的实际容量模型 (考虑实现损耗)
    implementation_loss = 0.8; % 20%实现损耗
    actual_capacity = theoretical_capacity * implementation_loss;
    
    % 频谱效率
    efficiency = actual_capacity / bandwidth;
    
    capacity_comparison(scenario_idx, :) = [theoretical_capacity, actual_capacity, efficiency];
end

%% 可视化结果
fig_shannon = figure('Name', 'Shannon限界分析', 'Position', [100, 100, 1200, 800]);

% 子图1: Shannon限界 vs SNR
subplot(2, 2, 1);
for scenario_idx = 1:num_scenarios
    plot(EbN0_dB, shannon_limits(:, scenario_idx)/1e6, 'LineWidth', 2, 'Color', colors(scenario_idx, :));
    hold on;
end
grid on;
xlabel('Eb/N0 (dB)');
ylabel('容量 (Mbps)');
title('不同信道场景的Shannon限界');
legend(channel_scenarios, 'Location', 'NorthWest');

% 子图2: 频谱效率比较
subplot(2, 2, 2);
efficiencies = capacity_comparison(:, 3);
bar(1:num_scenarios, efficiencies, 'FaceColor', colors(1, :));
set(gca, 'XTick', 1:num_scenarios, 'XTickLabel', channel_scenarios);
xlabel('信道场景');
ylabel('频谱效率 (bps/Hz)');
title(sprintf('频谱效率比较 (Eb/N0 = %d dB)', EbN0_dB(snr_comparison)));

% 子图3: 容量差距分析
subplot(2, 2, 3);
theoretical_capacities = capacity_comparison(:, 1) / 1e6;
actual_capacities = capacity_comparison(:, 2) / 1e6;
capacity_gap = theoretical_capacities - actual_capacities;

bar(1:num_scenarios, [actual_capacities, capacity_gap], 'stacked', 'FaceColor', colors(1, :));
legend({'实际容量', '容量差距'}, 'Location', 'NorthEast');
set(gca, 'XTick', 1:num_scenarios, 'XTickLabel', channel_scenarios);
xlabel('信道场景');
ylabel('容量 (Mbps)');
title('理论与实际容量差距');

% 子图4: Shannon限界 vs 实际系统
subplot(2, 2, 4);
% 比较理论限界与实际通信系统
actual_systems = {'2G', '3G', '4G', '5G'};
actual_efficiencies = [0.2, 0.5, 2.5, 10]; % bps/Hz (近似值)
theoretical_efficiency = log2(1 + 10^(EbN0_dB(snr_comparison)/10)); % AWGN理论值

% 绘制效率演进
plot(1:length(actual_systems), actual_efficiencies, 's-', 'LineWidth', 2, 'Color', colors(1, :));
hold on;
plot([0, length(actual_systems)+1], [theoretical_efficiency, theoretical_efficiency], ...
     'k--', 'LineWidth', 2);
grid on;
xlabel('移动通信系统');
ylabel('频谱效率 (bps/Hz)');
title('实际系统 vs Shannon限界');
legend({'实际系统', 'Shannon限界'}, 'Location', 'NorthWest');
set(gca, 'XTick', 1:length(actual_systems), 'XTickLabel', actual_systems);

fprintf('Shannon限界分析完成！\n');

end